/*
 * @copyright IMTT. Co. Ltd.
 * 
 * Here is detailed comment.
 */
package com.apis.web;

/**
 * @author ZhangMaoren
 * @date 2012-5-21
 * @copyright IMTT. Co. Ltd.
 */
import java.io.IOException;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Invoker {

	
	private String defaultPage;

	private Map<Integer, Action> actions;

	public Invoker() {
		actions = new HashMap<Integer, Action>();
	}

	public void register(int actionId, Action action) {
		actions.put(actionId, action);
	}

	public void execute(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		Action action = (Action) actions.get(Integer.parseInt(req
				.getParameter(WebConstants.TAG_ACTION_ID)));
		if (action != null) {
			action.execute(req, res);
		} else {
			RequestDispatcher dispatcher = req
					.getRequestDispatcher(defaultPage);
			dispatcher.forward(req, res);
		}
	}

	public String getDefaultPage() {
		return defaultPage;
	}

	public void setDefaultPage(String defaultPage) {
		this.defaultPage = defaultPage;
	}
}